package net.butfly.bus;

import java.io.Serializable;

import net.butfly.albacore.exception.BusinessException;
import net.butfly.albacore.exception.SystemException;

public class Error implements Serializable {
	private static final long serialVersionUID = -8461232615545890309L;
	private String code;
	private String message;
	private Error cause;
	private StackTraceElement[] stackTrace;

	public Error(Throwable ex) {
		if (ex instanceof SystemException) {
			SystemException e = (SystemException) ex;
			this.code = e.getCode();
			this.message = e.getMessage();
			this.stackTrace = e.getStackTrace();
			this.cause = new Error(e.getCause());
		} else if (ex instanceof BusinessException) {
			BusinessException e = (BusinessException) ex;
			this.code = e.getCode();
			this.message = e.getMessage();
			this.stackTrace = e.getStackTrace();
			this.cause = new Error(e.getCause());
		} else {
			this.code = null;
			this.message = ex.getMessage();
			this.stackTrace = ex.getStackTrace();
			this.cause = new Error(ex.getCause());
		}
	}

	protected Error(String code, String message) {
		this(code, message, null);
	}

	protected Error(String code, String message, StackTraceElement[] stackTraces) {
		this(code, message, null, null);
	}

	protected Error(String code, String message, StackTraceElement[] stackTraces, Error cause) {
		this.code = code;
		this.message = message;
		this.stackTrace = stackTraces;
		this.cause = cause;
	}

	public String getCode() {
		return code;
	}

	public String getMessage() {
		return message;
	}

	public StackTraceElement[] getStackTraces() {
		return stackTrace;
	}

	public Error getCause() {
		return cause;
	}
}
